const express = require('express');
const router = express.Router();
const { Article } = require('../models');
const { where } = require('sequelize');
const { failure, success } = require('../utils/responses');
const { NotFoundError } = require('../utils/errors');

//查询文章列表
router.get('/', async function (req, res) {
    try {
        const query = req.query;
        const currentPage = Math.abs(Number(query.currentPage)) || 1;
        const pageSize = Math.abs(Number(query.pageSize)) || 10;
        const offset = (currentPage - 1) * pageSize;

        const condition = {
            attributes: { exclude: ['content'] },
            order: [['id', 'DESC']],
            limit: pageSize,
            offset: offset
        }
        const { count, rows } = await Article.findAndCountAll(condition);

        success(res, '查询分类成功', {
            articles: rows,
            total: count,
            currentPage,
            pageSize
        })
    } catch (error) {
        failure(res, error)
    }
});

//查询文章详情
router.get('/:id', async function (req, res) {
    try {
        const { id } = req.params;
        const article = await Article.findByPk(id)
        if (!article) {
            throw new NotFoundError(`ID为${id}的文章不存在`)
        }

        success(res, '查询文章详情成功', { article })
    } catch (error) {
        failure(res, error)
    }
})
module.exports = router;
